<?php
namespace app\controller;
use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use \think\Session;
use think\cache\driver\Redis;

class Request_type
{
	/**
    *   判断是否请求类型正确
    */
    public function requestType($data_con)
    {
        $curlType = $_SERVER['REQUEST_METHOD'];
        $className =request()->action();
        if ($data_con[$className]!=$curlType) {
            return array("data"=>$className,"status"=>110001,"msg"=>"必须为".$data_con[$className]."请求");
        }
    }

    public function getUserInfo($token){
        $redis = new Redis();
        $redis->handler()->select(10);
        $userInfo = $redis->get($token);
        return $userInfo;
        // if($userInfo){
            
        // }else{
        //     return false;
        // }
        
    }

    /**
     * 图片上传方法
     * @return [type] [description]
     */
    public function upload($user = 'topic')
    {
        if($this->request->file('file')){
            $file = $this->request->file('file');
        }else{
            $res['code']=1;
            $res['msg']='没有上传文件';
            return json($res);
        }
        $web_config = Db::name('webconfig')->where('web','web')->find();
        $info = $file->validate(['size'=>$web_config['file_size']*1024,'ext'=>$web_config['file_type']])->rule('date')->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'user' . DS . $user);

        if($info) {
            $res['src'] = '/uploads/user/'.$user.'/' . date("Ymd",time())."/".$info->getFilename();
            $res['code'] = 2;
            return json($res);
        } else {
            // 上传失败获取错误信息
            return $this->error('上传失败：'.$file->getError());
        }
    }

    public function curl_post($url,$data=array()){
        $ch = curl_init();        
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;

    }

    public function curl_get($url,$postdata=[]) {
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_TIMEOUT,60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
        curl_setopt ($ch, CURLOPT_HTTPHEADER, [
            "Content-Type: application/json",
            ]);
        $output = curl_exec($ch);
        $info = curl_getinfo($ch);
        if (curl_error($ch)) {
            print "Error: " . curl_error($ch);
        }
        curl_close($ch);
        return $output;
    }
    /**
     * 获取客户端IP地址
     * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
     * @return mixed
     */
    public function get_client_ip($type = 0) {
        $type       =  $type ? 1 : 0;
        static $ip  =   NULL;
        if ($ip !== NULL) return $ip[$type];
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u",ip2long($ip));
        $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }

    /**
     * 获取钱包地址
     * @return [type] [description]
     */
    public function wallet_address(){

    }
    public function salt( $length = 8 )
    {
        // 密码字符集，可任意添加你需要的字符
        $chars = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9','!','@','#','$','%','^','&','*','(',')','-','_','[',']','{','}','<','>','~','`','+','=',',','.',';',':','/','?','|');
        // 在 $chars 中随机取 $length 个数组元素键名
        $keys = array_rand($chars, $length); 
        $salt = '';
        for($i = 0; $i < $length; $i++)
        {
            // 将 $length 个数组元素连接成字符串
            $salt .= $chars[$keys[$i]];
        }
        return $salt;
    }
    public function invite( $length = 8 )
    {
        // 密码字符集，可任意添加你需要的字符
        $chars = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');
        // 在 $chars 中随机取 $length 个数组元素键名
        $keys = array_rand($chars, $length); 
        $invite = '';
        for($i = 0; $i < $length; $i++)
        {
            // 将 $length 个数组元素连接成字符串
            $invite .= $chars[$keys[$i]];
        }
        return $invite;
    }
}